package com.ww.core.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ww.core.R;
import com.ww.core.util.StringUtils;
import com.ww.core.widget.slidemenu.BaseSlidingFragmentActivity;
import com.ww.core.widget.slidemenu.SlidingMenu;

/**
 * @version 2012-9-12
 * @author wangw
 */
public class SlideMenuActivity extends Fragment {
	public Activity context;
	private ProgressDialog mProgressDialog;//发送请求 等待的dialog

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = SlideMenuActivity.this.getActivity();
	}

	/**
	 * 初始化皮肤.
	 * @param title 标题栏文字
	 */
	public void setTitle(View view, String title) {
		try {
			TextView mTitleName = (TextView) view.findViewById(R.id.ivTitleName);
			mTitleName.setText(title);

			(view.findViewById(R.id.ivTitleBtnLeft)).setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					BaseSlidingFragmentActivity baseFragment = (BaseSlidingFragmentActivity) getActivity();
					SlidingMenu mSlidingMenu = baseFragment.getSlidingMenu();
					mSlidingMenu.showMenu(true);
				}
			});

		} catch (Exception e) {
		}
	}

	/**
	 * 初始化皮肤.
	 * @param title 标题栏文字
	 */
	public void setTitle(View view, int title) {
		setTitle(view, context.getString(title));
	}

	/**
	 * 设置右上角按钮图标.
	 * @param image 资源
	 */
	public void setRightButton(View view, int image, OnClickListener onClickListener) {
		try {
			ImageButton right = (ImageButton) view.findViewById(R.id.right);
			right.setImageResource(image);
			right.setVisibility(View.VISIBLE);
			right.setOnClickListener(onClickListener);
		} catch (Exception e) {
		}
	}

	public void failView(View view, String txt, int img) {
		ImageView imgerror = (ImageView) view.findViewById(R.id.imgerror);
		TextView neterror = (TextView) view.findViewById(R.id.neterror);
		LinearLayout nonetwork = (LinearLayout) view.findViewById(R.id.nonetwork);
		nonetwork.setVisibility(View.VISIBLE);
		if (img != 0) {
			imgerror.setImageResource(img);
		} else {
			imgerror.setVisibility(View.GONE);
		}
		if (StringUtils.isNotEmpty(txt)) {
			neterror.setText(txt);
		}
	}

	public void closeFailView(View view) {
		LinearLayout nonetwork = (LinearLayout) view.findViewById(R.id.nonetwork);
		nonetwork.setVisibility(View.GONE);
	}

	public void netView(View view) {
		LinearLayout nonetwork = (LinearLayout) view.findViewById(R.id.nonetwork);
		nonetwork.setVisibility(View.VISIBLE);
	}

	/**
	 * 创建进度框.
	 * @param mContext Context
	 */
	public void showProgressDialog(Context mContext, String tip) {
		if (mProgressDialog == null) {
			ProgressDialog dialog = new ProgressDialog(mContext);
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			mProgressDialog = dialog;
		}
		try {
			mProgressDialog.show();
			LayoutInflater inflater = LayoutInflater.from(context);
			View view = inflater.inflate(R.layout.core_loading, null);
			TextView textView = (TextView) view.findViewById(R.id.oaprogresstitle);
			String temp = mContext.getResources().getString(R.string.loading);
			if (StringUtils.isNotEmpty(tip)) {
				temp = tip;
			}
			textView.setText(temp);
			mProgressDialog.setContentView(view);
		} catch (Exception e) {
		}
	}

	/**
	 * 创建进度框.
	 * @param mContext Context
	 */
	public void showProgressDialog(Context mContext) {
		if (mProgressDialog == null) {
			ProgressDialog dialog = new ProgressDialog(mContext);
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			mProgressDialog = dialog;
		}
		try {
			mProgressDialog.show();
			mProgressDialog.setContentView(R.layout.core_loading);
		} catch (Exception e) {
		}
	}

	public void hideProgressDialog() {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
		}
	}

}
